package com.meirengu.channel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.channel.service.PACAService;
import com.meirengu.utils.HttpUtil;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.model.KwThirdback;
import com.meirengu.webview.service.KwThirdbackService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 平安投放
 *
 * @author Mao
 * @create 2017-12-15 12:11
 */
@Service
public class PACAServiceImpl implements PACAService {

    Logger logger = LoggerFactory.getLogger(PACAServiceImpl.class);
    @Autowired
    KwThirdbackService kwThirdbackService;

    /**
     * https://b.pingan.com.cn/station/activity/loan/loanapply/loantrust.html?source=sa0000371&outerSource=os0002748
     *
     * @param
     * @return
     */

    public Integer applyLoan(String userName, String userAge, String userPhone, String city,
                             String userCreditCard, String liveTime, String houseLoan,
                             String userCar, String insurancePolicy) throws Exception {

        String url = ConfigUtil.getConfig("paca.url");
        String source = ConfigUtil.getConfig("paca.source");
        String outerSource = ConfigUtil.getConfig("paca.outerSource");
        String cityStr = ConfigUtil.getConfig(city);
        String cityComplate = null;
        String cityCode = null;
        if (StringUtils.isNotBlank(cityStr)) {
            String[] cityStrArr = cityStr.split("&");
            if (cityStrArr != null && cityStrArr.length > 0) {
                cityComplate = cityStrArr[0];
                cityCode = cityStrArr[1];
            }
        }

        Map<String, String> params = new HashMap<>();
        params.put("userName", userName);
        params.put("userAge", userAge);
        params.put("userPhone", userPhone);
        params.put("city", cityComplate);
        params.put("cityCode", cityCode);
        params.put("source", source);
        params.put("outerSource", outerSource);
        params.put("channel", "01");//默认为01  01：综合获客贷款H5；
        params.put("userCreditCard", userCreditCard);
        params.put("liveTime", liveTime);
        params.put("houseLoan", houseLoan);
        params.put("userCar", userCar);
        params.put("insurancePolicy", insurancePolicy);

        //接口调用信息表
        KwThirdback thirdback = new KwThirdback();
        thirdback.setInterfaceCode("newloan");
        thirdback.setInterfaceName("平安新一贷注册接口");
        thirdback.setMobile(userPhone);
        thirdback.setSendUrl(url.toString());
        thirdback.setParams(params.toString());
        thirdback.setCode(Constants.BUSINESS_STATUS_FAIL);

        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPostForm(url, params);
            int code = hr.getStatusCode();
            if (code == 200) {
                thirdback.setBackUrl(hr.toString());
                thirdback.setHttpCode(200);
                thirdback.setHttpContent(hr.toString());

                logger.info("==PACAServiceImpl.applyLoan request success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("responseCode");
                String rMsg = returnMsg.getString("responseMsg");
                if ("000000".equals(rCode)) {
                    thirdback.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    thirdback.setMessage("成功");
                    logger.info("==PACAServiceImpl.applyLoan business success");
                } else if ("900115".equals(rCode)) {
                    thirdback.setCode(Constants.BUSINESS_STATUS_OUT_LIMIT);
                    thirdback.setMessage(rMsg);
                    logger.info("==PACAServiceImpl.applyLoan business fail");
                } else {
                    thirdback.setCode(Constants.BUSINESS_STATUS_OTHER);
                    thirdback.setMessage("rCode: " + rCode + " rMsg" + rMsg);
                    logger.warn("==PACAServiceImpl.applyLoan business fail");
                }
            } else {
                thirdback.setBackUrl(hr.toString());
                thirdback.setHttpCode(code);
                thirdback.setHttpContent(hr.toString());
                logger.warn("==PACAServiceImpl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            thirdback.setHttpCode(408);
            thirdback.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwThirdbackService.insert(thirdback);
            return thirdback.getCode();
        }

    }

}
